import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { UserService } from '@core/routes-services/user.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { USER_ROLE_OPTION } from '../../constant/dictionary';
import { CurrUserService } from '@core/routes-services/Observer-service/curr-user.service';
import { CacheService } from '@core/routes-services/cache.service';
import { LoginService } from '@core/routes-services/login.service';
import { CurrUserModel } from '@core/routes-services/Observer-service/curr-user.model';
import { TriggerService } from '@core/routes-services/trigger.service';

@Component({
  selector: 'app-curruser-info',
  templateUrl: './curruser-info.component.html',
})
export class CurruserInfoComponent implements OnInit {

  entity: any = {};
  validateForm: FormGroup;
  userRoleOption: any [] = USER_ROLE_OPTION;
  isLoading: boolean = false;

  constructor(private fb: FormBuilder,
              public userService: UserService,
              public triggerService: TriggerService,
              private loginService: LoginService,
              private currUserService: CurrUserService,
              private cacheService: CacheService,
              public message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.entity  = this.cacheService.getCurrUserInfo();
    this.refreshCurrUserInfo();
    this.initValidateForm();
  }

  /**
   * 到个人中心刷新个人信息
   */
  refreshCurrUserInfo(){
    this.loginService.getCurrUserInfo().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.entity = res.data;
        this.initValidateForm();
      } else {
        this.message.warning(res.message);
      } }, error => {
      this.message.error(error.message ? error.message : error.error)
    });
  }

  initValidateForm(){
    this.validateForm = this.fb.group({
      name: [this.entity.name, [Validators.required, Validators.maxLength(30)]],
      role: [this.entity.role, [Validators.required]],
      email: [this.entity.email, [Validators.email]],
    });
  }

  submitForm(): void {
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      this.isLoading = true;
      this.userService.saveOrUpdateEntity(this.entity.id, this.validateForm.value).then((res) => {
        if (res.status.toString().startsWith("20")) {
          this.cacheService.setCurrUserInfo(res.data);
          this.currUserService.setCurrUser(new CurrUserModel(res.data.id, res.data.name, res.data.role));
          this.message.success("保存成功");
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error)
        this.isLoading = false;
      });
    }
  }


}
